<?php
class retrieve_category_admin extends retrieve_base{

	private $module_display_options 	= array(
//		array("CATEGORY_DISPLAY",	LOCALE_DISPLAY_CATEGORY)
	);
	
		/**
	* Add virtual Folders to clients
    *
    * if the client does not have any virtual folders define this will create some.
    */
	function update_modules_add_v_folders(){
		$sql = "SELECT * FROM client left outer join category_belongs_to_module on cbtm_client = client_identifier and cbtm_module='FILES_' where cbtm_client is null";
		$result  = $this->db->query($sql);
        while($r = $this->db->fetch_array($result)){
        	$this->create_client_details(
				Array("client_identifier" => $r["client_identifier"])
			);
        }
        $this->db->free_result($result);
	}
	function create_client_details($parameters){
		$client_identifier = $this->check_parameters($parameters,"client_identifier",-1);
		/**
		* create reference for enterprise clients for FILES_ module to have specific category list
		*/
		
		$cat_identifier = $this->getUid();
		$sql="insert into category (cat_identifier, cat_client, cat_label, cat_parent, cat_list_id) values ($cat_identifier, $client_identifier, 'File Uploads (Virtual Folder List)',-1, $cat_identifier);";
		$this->db->query($sql);

		/**
		* create two virtual folders for starters
		-
		* we do not need to extract these as they are in the root of the category list
		* File uploaders can add new sub folders to these two at will.
		*/
		$clist = $cat_identifier;
		$cat_identifier = $this->getUid();
		$sql="insert into category (cat_identifier, cat_client, cat_label, cat_parent, cat_list_id) values ($cat_identifier, $client_identifier, 'Images', $clist, $clist);";
		$this->db->query($sql);
		$cat_identifier = $this->getUid();
		$sql="insert into category (cat_identifier, cat_client, cat_label, cat_parent, cat_list_id) values ($cat_identifier, $client_identifier, 'Documents', $clist, $clist);";
		$this->db->query($sql);
		/**
		* tell the File Manager to use this category list as virtual folders
		*/
		$sql="insert into category_belongs_to_module (cbtm_client, cbtm_clist, cbtm_module) values ($client_identifier,$clist,'FILES_');";
		$this->db->query($sql);
	}
	/**
	* function produces abstract structure of modules db structure
	*
    * used to generate the table structure required abstract function that passes an abstract
	* representation of the desired table structure to the proper database module which will
	* interpet the abstract and convert it into a valid SQL Create table structure.
	*
	* @return Array list of abstract table definitions for this module
	*/
	function create_table(){
		if ($this->parent->module_type!="install"){
			return "";
		}

		$tables = array();
		/**
		* Table structure for table 'category'
		*
		* the category table holds the list of categories
		*/
		$fields = array(
			array("cat_identifier"		,"unsigned integer"			,"NOT NULL"	,"auto_increment","key"),
			array("cat_client"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("cat_label"			,"varchar(255)"				,"NOT NULL"	,"default '0'"),
			array("cat_parent"			,"integer"					,"NOT NULL"	,"default '0'","key"),
			array("cat_list_id"			,"unsigned integer"			,"NOT NULL"	,"default '0'","key")
		);

		$primary ="cat_identifier";
		$tables[count($tables)] = array("category", $fields, $primary);

		/**
		* Table structure for table 'category_belongs_to_module'
		*
		* the category_belongs_to_module table allows a module to access specific lists of categories
		*/
		$fields = array(
			array("cbtm_identifier"		,"unsigned integer"			,"NOT NULL"	,"auto_increment","key"),
			array("cbtm_client"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("cbtm_module"			,"varchar(255)"				,"NOT NULL"	,"default '0'"),
			array("cbtm_clist"			,"unsigned integer"			,"NOT NULL"	,"default '0'")
		);

		$primary ="cbtm_identifier";
		$tables[count($tables)] = array("category_belongs_to_module", $fields, $primary);
		/**
		* Table structure for table 'category_to_object'
		*
		* the category_to_object table allows a module to store lists of categories that belong to
		* an object in that module
		*/
		$fields = array(
			array("cto_identifier"		,"unsigned integer"			,"NOT NULL"	,"auto_increment"	,"key"),
			array("cto_client"			,"unsigned integer"			,"NOT NULL"	,"default '0'"		,"key"),
			array("cto_object"			,"unsigned integer"			,"NOT NULL"	,"default '0'"		,"key"),
			array("cto_module"			,"varchar(255)"				,"NOT NULL"	,"default '0'"		,"key"),
			array("cto_clist"			,"unsigned integer"			,"NOT NULL"	,"default '0'"		,"key")
		);

		$primary ="cto_identifier";
		$tables[count($tables)] = array("category_to_object", $fields, $primary);
		/**
		* Table structure for table 'category_ranking'
		*
		* the category_ranking table allows a module to store a ranked order of categories
		*/
		$fields = array(
			array("cr_identifier"	,"unsigned integer"			,"NOT NULL"	,"auto_increment"	,"key"),
			array("cr_client"		,"unsigned integer"			,"NOT NULL"	,"default '0'"	,"key"),
			array("cr_cat"			,"unsigned integer"			,"NOT NULL"	,"default '0'"	,"key"),
			array("cr_rank"			,"unsigned integer"			,"NOT NULL"	,"default '0'"	,"key"),
			array("cr_link"			,"unsigned integer"			,"NOT NULL"	,"default '0'"	,"key"),
			array("cr_module"		,"varchar(25)"				,"NOT NULL"	,"default ''"	,"key")
		);

		$primary ="cr_identifier";
		$tables[count($tables)] = array("category_ranking", $fields, $primary);
		/**
		* Table structure for table 'category_list_settings'
		*
		* the category_ranking table allows a module to store a ranked order of categories
		*/
		$fields = array(
			array("cls_identifier"	,"unsigned integer"			,"NOT NULL"	,"default '0'"	,"key"),
			array("cls_client"		,"unsigned integer"			,"NOT NULL"	,"default '0'"	,"key"),
			array("cls_cat"			,"unsigned integer"			,"NOT NULL"	,"default '0'"	,"key"),
			array("cls_rank_type"	,"unsigned integer"			,"NOT NULL"	,"default '0'"	,"key")
		);

		$primary ="cls_identifier";
		$tables[count($tables)] = array("category_list_settings", $fields, $primary);


		return $tables;
	}
}
?>